Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer require_relative for internal requires #831

Closed
wants to merge 1 commit into from

Conversation

tagliala
Copy link
Contributor

@tagliala tagliala commented Sep 21, 2024

require_relative is preferred over require for files within the same project because it uses paths relative to the current file, making code more portable and less dependent on the load path.

This change updates internal requires to use require_relative for consistency, performance, and improved portability.

Refs:

To do

  • I have read the contributing guidelines.
  • I have added relevant tests.
  • I have adjusted relevant documentation.
  • I have made sure the individual commits are meaningful.
  • I have added relevant lines to the CHANGELOG.

PS: Thank you for contributing to Pundit ❤️


I think this does not deserve an entry in the changelog

Relevant comment: rubocop/rubocop#8748 (comment)

`require_relative` is preferred over `require` for files within the same
project because it uses paths relative to the current file, making code
more portable and less dependent on the load path.

This change updates internal requires to use `require_relative` for
consistency, performance, and improved portability.

Refs:
- rubocop/rubocop#8748
@Burgestrand
Copy link
Member

Burgestrand commented Sep 23, 2024

Thank you for the PR. I read through the discussion in Rubocop. I didn't see any compelling arguments for why this would be better.

I'm of the opinion that relying on the load path is a Good Thing, specifically because it allows flexibility.

To be even more specific, if you for any reason wanted to prevent Pundit from loading e.g. pundit/policy_finder when we're using require, then you can do so. For example by creating ./pundit/policy_finder.rb and then $LOAD_PATH.unshift ".". Why would you want to? I don't know, but frankly I've needed this at least three times over the years, so it's an opinion I took upon through experience.

Using require_relative would strip away this flexibility, but I don't see a benefit that compensates for that loss.

I'm open to hearing solid arguments about this, but without that this is a hard pass.

@tagliala
Copy link
Contributor Author

Thanks, can you share the concerns on the rubocop discussion? It may help others in deciding

@Burgestrand
Copy link
Member

Thanks, can you share the concerns on the rubocop discussion? It may help others in deciding

That's a good suggestion, thank you!

@Burgestrand
Copy link
Member

rubocop/rubocop#8748 (comment)

I wrote a comment. Thank you for the push!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants